#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;

void handler(int signum)
{
    while (1)
    {
        pid_t pid = waitpid(-1, NULL, WNOHANG);
        if (pid == 0)
        {
            break;
        }
        else if (pid < 0)
        {
            break;
        }
    }
    cout << "father process get a child process end signal" << signum << endl;
}

int main()
{
    signal(SIGCHLD, handler);
    //segne

    for (int i = 0; i < 10; i++)
    {
        pid_t pid = fork();
        if (pid < 0)
        {
            cout << "fork error" << endl;
            return -1;
        }
        if (pid == 0)
        {
              
            cout << "i am child process" << endl;
              sleep(2);
            if (i <= 6)
            {
                exit(3);
            }
            else
             pause();
        }
    }
    while (true)
    {
        cout << "father process exit!" << endl;
        sleep(1);
    }
    return 0;
}